/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.homepage.smile.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.homepage.smile.client.SmileServiceAsync;
import com.google.gwt.homepage.smile.client.event.EnneagramGoResultEvent;
import com.google.gwt.homepage.smile.client.presenter.EnneagramTestPresenter.Display;
import com.google.gwt.homepage.smile.shared.EnneagramTestResultBean;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A Composite widget that abstracts a DynaTableWidget and a data provider tied
 * to the <@link SchoolCalendarService> RPC endpoint.
 */
public class EnneagramTestDoPresenter extends Composite  implements Presenter {

	
	 public interface Display {
		    HasClickHandlers getResultButton();
		    //HasClickHandlers getDeleteButton();
		    //HasClickHandlers getList();
		    void showDialog();
		    boolean getAllChecked();
		    //List<Integer> getSelectedRows();
			//HasWidgets getContentWidget();//컨텐츠가 들어갈 부분을 가져옴( 이 영역에 EnneagramTestWidget, EnneagramResultWidget가 들어감
			//EnneagramTestResultBean getResult();//결과값 가져오기
			void showTestResult();
		    Widget asWidget();
		  }
	
	


	  private final SmileServiceAsync rpcService;
	  private final HandlerManager eventBus;
	  private final Display display;

    
    
    public void go(final HasWidgets container) {
        bind();
        container.clear();
        //System.out.println("5555");
        
        container.add(display.asWidget());
        //System.out.println("6666");
       
      }
    
    
    public void bind() {
    	  
    	  
        display.getResultButton().addClickHandler(new ClickHandler() {   
          public void onClick(ClickEvent event) {
        	  showResult();
            
          }
        });
        /*
        display.getDeleteButton().addClickHandler(new ClickHandler() {   
          public void onClick(ClickEvent event) {
            deleteSelectedContacts();
          }
        });
        
        display.getList().addClickHandler(new ClickHandler() {
          public void onClick(ClickEvent event) {
            int selectedRow = display.getClickedRow(event);
            
            if (selectedRow >= 0) {
              String id = contactDetails.get(selectedRow).getId();
              eventBus.fireEvent(new EditContactEvent(id));
            }
          }
        });*/
      }
    


  
                            
  
  public EnneagramTestDoPresenter(SmileServiceAsync rpcService, HandlerManager eventBus, Display view) {
	    this.rpcService = rpcService;
	    this.eventBus = eventBus;
	    this.display = view;
	  }
  
 
  
  //결과화면으로 전환
  private void showResult(){
	  //모두 선택했는지 확인
	  boolean allAnswer = display.getAllChecked();
	  
  	
  	if(!allAnswer){
  		display.showDialog();
  	}else{
	    //System.out.println("00000000000000");
  		//eventBus.fireEvent(new EnneagramGoResultEvent(display.getResult()));
  		
  		display.showTestResult();
	 
  	}
  }  
  
  
  
  /**
   * Create the dialog box for this example.
   *
   * @return the new dialog box
   */
  private DialogBox createDialogBox() {
    // Create a dialog box and set the caption text
    final DialogBox dialogBox = new DialogBox();
    //dialogBox.ensureDebugId("cwDialogBox");
    dialogBox.setText("확인");
   
    dialogBox.setPixelSize(250, 70);
    
    // Create a table to layout the content
    VerticalPanel dialogContents = new VerticalPanel();
    dialogContents.setSpacing(4);
    dialogBox.setWidget(dialogContents);

    // Add some text to the top of the dialog
    HTML details = new HTML("답변하지 않은 문항이 있습니다!");
    dialogContents.add(details);
    dialogContents.setCellHorizontalAlignment(
        details, HasHorizontalAlignment.ALIGN_CENTER);

   
  
    
    
    // Add a close button at the bottom of the dialog
    Button closeButton = new Button(
        "닫기", new ClickHandler() {
          public void onClick(ClickEvent event) {
            dialogBox.hide();
          }
        });
    dialogContents.add(closeButton);
    
    if (LocaleInfo.getCurrentLocale().isRTL()) {
      dialogContents.setCellHorizontalAlignment(
          closeButton, HasHorizontalAlignment.ALIGN_LEFT);

    } else {
      dialogContents.setCellHorizontalAlignment(
          closeButton, HasHorizontalAlignment.ALIGN_RIGHT);
    }
	
    // Return the dialog box
    return dialogBox;
  }
  
 
}
